package com.metaera.app.ui.popup;

import android.content.Context;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.lxj.xpopup.core.BottomPopupView;
import com.metaera.app.LocalConfigStore;
import com.metaera.app.R;
import com.metaera.app.controller.presenter.CategoryPresenter;
import com.metaera.app.controller.view.core.INetworkRequestView;
import com.metaera.app.data.model.HomeNewsTypeResponse;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.SwitchCompat;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

public class FlashNewsCategoryPopup extends BottomPopupView implements INetworkRequestView<List<HomeNewsTypeResponse>> {

    private ViewPager    viewPager;
    private RecyclerView recyclerView;

    public FlashNewsCategoryPopup(@NonNull Context context) {
        super(context);
    }

    public FlashNewsCategoryPopup(@NonNull Context context, ViewPager viewPager) {
        super(context);
        this.viewPager = viewPager;
    }

    @Override
    protected int getImplLayoutId() {

        return R.layout.popup_flash_news_category;
    }

    @Override
    protected void onCreate() {
        super.onCreate();

        recyclerView = findViewById(R.id.recycler_view);

        CategoryPresenter presenter = new CategoryPresenter();
        presenter.setModelAndView(this);
        presenter.flashNews();

        // 关闭
        findViewById(R.id.button_close).setOnClickListener(v -> {
            // 更改位置
            dismiss();
        });

        SwitchCompat switchCompat = findViewById(R.id.switch_compat);
        switchCompat.setChecked(LocalConfigStore.getInstance().isOnlyFlashTitle());
        switchCompat.setOnCheckedChangeListener((buttonView, isChecked) ->
                LocalConfigStore.getInstance().onlyTitle(isChecked));
    }

    @Override
    public void successful(List<HomeNewsTypeResponse> data) {
        BaseQuickAdapter<HomeNewsTypeResponse, BaseViewHolder> adapter =
                new BaseQuickAdapter<HomeNewsTypeResponse, BaseViewHolder>(R.layout.item_popup_home_category, data) {
            @Override
            protected void convert(@NonNull BaseViewHolder holder, HomeNewsTypeResponse item) {
                holder.setText(R.id.tv_name, item.getName());
            }
        };

        adapter.setOnItemClickListener((_a, view, position) -> {
            viewPager.setCurrentItem(position);
            dismiss();
        });

        recyclerView.setAdapter(adapter);
    }

}
